package com.ysw.rpc.client.utils;

import com.ysw.rpc.client.interceptor.ProxyMethodInterceptor;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author YinShangwen
 * @since 2023/4/10 13:46
 */
public class BeanUtil {

    private final static Map<Class<?>, Object> PROXY_CACHE_MAP = new ConcurrentHashMap<>();

    /**
     * 创建代理对象-单例
     *
     * @param clazz clazz
     * @return proxy
     */
    public static Object createProxy(Class<?> clazz) {
        if (!PROXY_CACHE_MAP.containsKey(clazz)) {
            synchronized (PROXY_CACHE_MAP) {
                if (!PROXY_CACHE_MAP.containsKey(clazz)) {
                    PROXY_CACHE_MAP.put(clazz, new ProxyMethodInterceptor(clazz).getInstance());
                }
            }
        }
        return PROXY_CACHE_MAP.get(clazz);
    }
}
